<template>
    <h1>登录页面</h1>

    <table>
        <tr>
            <td>账号</td>
            <td>
                <input type="text" v-model="name"  />
            </td>
        </tr>

        <tr>
            <td>密码</td>
            <td>
                <input type="password" v-model="pwd" />
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="登录" @click="Login" />
            </td>
        </tr>
    </table>

</template>

<script setup lang="ts">

import {ref} from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';

const name = ref('');
const pwd = ref('');

const router = useRouter();


//登录功能
const Login = ()=>{
    axios({
        method:'get',
        url:'http://localhost:62821/api/User/Login',
        params:{
            name:name.value,
            pwd:pwd.value
        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data.length>0){
            alert('用户已登录');
            State(res.data[0].U_Id);
        }
        else{
            alert('登录失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

//根据用户编号获取状态
const State =(Id:number)=>{
    axios({
        method:'get',
        url:'http://localhost:62821/api/User/UserState',
        params:{
            Id:Id
        }
    })
    .then(res=>{
        console.log(res.data);
        if(res.data[0].R_Id==2){
            alert('管理员你好');
            router.push('/AddUser');
        }
        else{
            alert('用户你好');
            router.push({
                name:'UserInfo',
                params:{
                    Id:Id
                }
            })
        }
    })
}

</script>